package com.sql.helper;

import com.sql.bean.Student;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBService {
	private DBHelper dbHelper;

	public DBService(Context context){
		this.dbHelper = new DBHelper(context);
	}

	// 插入操作
	public void Insert(Student stu) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("insert into student(name,age,sex) values(?,?,?)",
				new Object[] {stu.getName(), stu.getAge(), stu.getSex() });
	}
	//删除操作
	public void Delete(String name){
		
	}
	
	//查询操作
	public Student Select(Integer id){
		//单行数据的查询
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Student stu=null;
		// db.rawQuery()返回 一个Cursor对象，这个对象类似于JavaSE中的ResultSet结果集
		Cursor cursor=db.rawQuery("select * from student where sid=?",new String[]{id+""});
		while(cursor.moveToFirst()){
			int sid=cursor.getInt(cursor.getColumnIndex("sid"));
			Log.v("Select", sid+"");
			String name=cursor.getString(cursor.getColumnIndex("name"));
			int age=cursor.getInt(cursor.getColumnIndex("age"));
			String sex=cursor.getString(cursor.getColumnIndex("sex"));
			stu=new Student(name, age, sex);
		}
		cursor.close();
		return stu;
		
	}
	
	public Cursor GetCursorData(int startRow, int rowCount) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select sid as _id,name,age,sex from student limit ?,?",
				new String[] { startRow + "", rowCount + "" });
	
		return cursor;
	}

}
